﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Travel.DAL
{
    /// <summary>
    /// 数据访问类:route_price
    /// </summary>
    public partial class route_price
    {
        public route_price()
        { }
        #region  Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(Guid id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from route_price");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.UniqueIdentifier,16)			};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Travel.Model.route_price model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into route_price(");
            strSql.Append("id,code,route_id,adult_price,children_price,total_numbers,sale_numbers,userd_date,account_id,created_on,created_by,changed_on,changed_by)");
            strSql.Append(" values (");
            strSql.Append("@id,@code,@route_id,@adult_price,@children_price,@total_numbers,@sale_numbers,@userd_date,@account_id,@created_on,@created_by,@changed_on,@changed_by)");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@code", SqlDbType.NVarChar,500),
					new SqlParameter("@route_id", SqlDbType.Int,4),
					new SqlParameter("@adult_price", SqlDbType.Decimal,9),
					new SqlParameter("@children_price", SqlDbType.Decimal,9),
					new SqlParameter("@total_numbers", SqlDbType.Int,4),
					new SqlParameter("@sale_numbers", SqlDbType.Int,4),
					new SqlParameter("@userd_date", SqlDbType.Date,3),
					new SqlParameter("@account_id", SqlDbType.Int,4),
					new SqlParameter("@created_on", SqlDbType.DateTime),
					new SqlParameter("@created_by", SqlDbType.NVarChar,50),
					new SqlParameter("@changed_on", SqlDbType.DateTime),
					new SqlParameter("@changed_by", SqlDbType.NVarChar,50)};
            parameters[0].Value = Guid.NewGuid();
            parameters[1].Value = model.code;
            parameters[2].Value = model.route_id;
            parameters[3].Value = model.adult_price;
            parameters[4].Value = model.children_price;
            parameters[5].Value = model.total_numbers;
            parameters[6].Value = model.sale_numbers;
            parameters[7].Value = model.userd_date;
            parameters[8].Value = model.account_id;
            parameters[9].Value = model.created_on;
            parameters[10].Value = model.created_by;
            parameters[11].Value = model.changed_on;
            parameters[12].Value = model.changed_by;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Travel.Model.route_price model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update route_price set ");
            strSql.Append("code=@code,");
            strSql.Append("route_id=@route_id,");
            strSql.Append("adult_price=@adult_price,");
            strSql.Append("children_price=@children_price,");
            strSql.Append("total_numbers=@total_numbers,");
            strSql.Append("sale_numbers=@sale_numbers,");
            strSql.Append("userd_date=@userd_date,");
            strSql.Append("account_id=@account_id,");
            strSql.Append("created_on=@created_on,");
            strSql.Append("created_by=@created_by,");
            strSql.Append("changed_on=@changed_on,");
            strSql.Append("changed_by=@changed_by");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@code", SqlDbType.NVarChar,500),
					new SqlParameter("@route_id", SqlDbType.Int,4),
					new SqlParameter("@adult_price", SqlDbType.Decimal,9),
					new SqlParameter("@children_price", SqlDbType.Decimal,9),
					new SqlParameter("@total_numbers", SqlDbType.Int,4),
					new SqlParameter("@sale_numbers", SqlDbType.Int,4),
					new SqlParameter("@userd_date", SqlDbType.Date,3),
					new SqlParameter("@account_id", SqlDbType.Int,4),
					new SqlParameter("@created_on", SqlDbType.DateTime),
					new SqlParameter("@created_by", SqlDbType.NVarChar,50),
					new SqlParameter("@changed_on", SqlDbType.DateTime),
					new SqlParameter("@changed_by", SqlDbType.NVarChar,50),
					new SqlParameter("@id", SqlDbType.UniqueIdentifier,16)};
            parameters[0].Value = model.code;
            parameters[1].Value = model.route_id;
            parameters[2].Value = model.adult_price;
            parameters[3].Value = model.children_price;
            parameters[4].Value = model.total_numbers;
            parameters[5].Value = model.sale_numbers;
            parameters[6].Value = model.userd_date;
            parameters[7].Value = model.account_id;
            parameters[8].Value = model.created_on;
            parameters[9].Value = model.created_by;
            parameters[10].Value = model.changed_on;
            parameters[11].Value = model.changed_by;
            parameters[12].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(Guid id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from route_price ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.UniqueIdentifier,16)			};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from route_price ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Travel.Model.route_price GetModel(Guid id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,code,route_id,adult_price,children_price,total_numbers,sale_numbers,userd_date,account_id,created_on,created_by,changed_on,changed_by from route_price ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.UniqueIdentifier,16)			};
            parameters[0].Value = id;

            Travel.Model.route_price model = new Travel.Model.route_price();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = new Guid(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["code"] != null && ds.Tables[0].Rows[0]["code"].ToString() != "")
                {
                    model.code = ds.Tables[0].Rows[0]["code"].ToString();
                }
                if (ds.Tables[0].Rows[0]["route_id"] != null && ds.Tables[0].Rows[0]["route_id"].ToString() != "")
                {
                    model.route_id = int.Parse(ds.Tables[0].Rows[0]["route_id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["adult_price"] != null && ds.Tables[0].Rows[0]["adult_price"].ToString() != "")
                {
                    model.adult_price = decimal.Parse(ds.Tables[0].Rows[0]["adult_price"].ToString());
                }
                if (ds.Tables[0].Rows[0]["children_price"] != null && ds.Tables[0].Rows[0]["children_price"].ToString() != "")
                {
                    model.children_price = decimal.Parse(ds.Tables[0].Rows[0]["children_price"].ToString());
                }
                if (ds.Tables[0].Rows[0]["total_numbers"] != null && ds.Tables[0].Rows[0]["total_numbers"].ToString() != "")
                {
                    model.total_numbers = int.Parse(ds.Tables[0].Rows[0]["total_numbers"].ToString());
                }
                if (ds.Tables[0].Rows[0]["sale_numbers"] != null && ds.Tables[0].Rows[0]["sale_numbers"].ToString() != "")
                {
                    model.sale_numbers = int.Parse(ds.Tables[0].Rows[0]["sale_numbers"].ToString());
                }
                if (ds.Tables[0].Rows[0]["userd_date"] != null && ds.Tables[0].Rows[0]["userd_date"].ToString() != "")
                {
                    model.userd_date = DateTime.Parse(ds.Tables[0].Rows[0]["userd_date"].ToString());
                }
                if (ds.Tables[0].Rows[0]["account_id"] != null && ds.Tables[0].Rows[0]["account_id"].ToString() != "")
                {
                    model.account_id = int.Parse(ds.Tables[0].Rows[0]["account_id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["created_on"] != null && ds.Tables[0].Rows[0]["created_on"].ToString() != "")
                {
                    model.created_on = DateTime.Parse(ds.Tables[0].Rows[0]["created_on"].ToString());
                }
                if (ds.Tables[0].Rows[0]["created_by"] != null && ds.Tables[0].Rows[0]["created_by"].ToString() != "")
                {
                    model.created_by = ds.Tables[0].Rows[0]["created_by"].ToString();
                }
                if (ds.Tables[0].Rows[0]["changed_on"] != null && ds.Tables[0].Rows[0]["changed_on"].ToString() != "")
                {
                    model.changed_on = DateTime.Parse(ds.Tables[0].Rows[0]["changed_on"].ToString());
                }
                if (ds.Tables[0].Rows[0]["changed_by"] != null && ds.Tables[0].Rows[0]["changed_by"].ToString() != "")
                {
                    model.changed_by = ds.Tables[0].Rows[0]["changed_by"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,code,route_id,adult_price,children_price,total_numbers,sale_numbers,userd_date,account_id,created_on,created_by,changed_on,changed_by ");
            strSql.Append(" FROM route_price ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,code,route_id,adult_price,children_price,total_numbers,sale_numbers,userd_date,account_id,created_on,created_by,changed_on,changed_by ");
            strSql.Append(" FROM route_price ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM route_price ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from route_price T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "route_price";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

